import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1, y2 = np.sin(x), np.cos(x)

# 使用画布和子图
fig, axs = plt.subplots()  # 生产1个子图
axs.plot(x, y1, x, y2)
# axs.set_xlabel("x轴")
# axs.set_ylabel("y轴")
axs.set_xlim(x.min() * 1.5, x.max() * 1.5)  # 设置x轴刻度范围
axs.set_xticks(
    [-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi], ["-π", "-π/2", "0", "π/2", "π"]
)  # 设置x轴刻度标签
axs.set_title("正弦曲线和余弦曲线", fontsize=20)

# 设置图例
v_labels = ["正弦曲线", "余弦曲线"]
axs.legend(v_labels, loc="upper right", shadow=True, fancybox=True)

# # 显示网格
# axs.grid(True, which="major", color="gray", axis="y", linestyle="-", linewidth=0.5)


axs.spines["right"].set_visible(False)
axs.spines["top"].set_visible(False)
axs.spines["bottom"].set_position("zero")
axs.spines["left"].set_position("zero")

# # 显示参考线
# axs.axhline(0, color="black", linestyle="--", linewidth=1.0)
# axs.axvline(0, color="black", linestyle="--", linewidth=1.0)

# # 显示参考区域
# axs.axhspan(0.5, 1, facecolor="gray", alpha=0.3)
# axs.axvspan(-1, 1, facecolor="gray", alpha=0.3)

# # 设置指向性注释文本
# axs.annotate(
#     "最小值",
#     (-np.pi / 2, -1),
#     xycoords="data",
#     xytext=(-np.pi / 2, -0.5),
#     arrowprops=dict(arrowstyle="->"),
# )

# # 设置无指向性注释文本
# axs.text(np.pi, 0.20, "y=sin(x)", bbox=dict(facecolor="red", alpha=0.5), fontsize=12)

# 设置中文用楷体
plt.rcParams["font.sans-serif"] = ["KaiTi"]
plt.rcParams["axes.unicode_minus"] = False  # 解决负号'-'显示为方块的问题

# 显示图形
plt.show()
